COSC 220 Computer Science II

Sorting Algorithm Performance Analysis


This is a lab for practicing the sorting algorithm and algorithm performance analysis. You will implement the following sorting routines (you can go to Internet to find implementation). Each function is designed to take a vector of numbers (random generated before each search) and order the elements into ascending order.

• Bubble sort, Insertion sort, Merge sort, Quicksort, Selection sort, Radix sort, Heap sort

Experiment Design

You will need to run and measure performance of the above algorithm for vector size 10, 20, 30, 40, 50, 100, 500, 1000, 5000, 10000, 100000, 500000 and record the time for each case in a table like below:

size Bubble Insertion Merge Quicksort Selection  Radix  Heap
               
               
               
               
               
               
               
               
               

Timing an operation

One way to measure elapsed system time for programs is to use the standard clock function, which is exported by the standard ctime interface. The clock function takes no arguments and returns the amount of time the processing unit of the computer has used in the execution of the program. The unit of measurement and even the type used to store the result of clock differ depending on the type of machine, but you can always convert the system-dependent clock units into seconds by using the following expression:

double(clock()) / CLOCKS_PER_SEC

If you record the starting and finishing times in the variables start and finish, you can use the following code to compute the time required by a calculation:

#include <ctime>

int main()

{      

double start = double(clock()) / CLOCKS_PER_SEC;

. . . Perform some calculation . . .

double finish = double(clock()) / CLOCKS_PER_SEC;

double elapsed = finish - start;

}

Unfortunately, calculating the time requirements for a program that runs quickly requires some subtlety because there is no guarantee that the system clock unit is precise enough to measure the elapsed time. For example, if you used this strategy to time the process of sorting 10 integers, the odds are good that the time value of elapsed at the end of the code fragment would be 0. The reason is that the processing unit on most machines can execute many instructions in the space of a single clock tick—almost certainly enough to get the entire sorting process done for 10 elements. Because the system’s internal clock may not tick in the interim, the values recorded for start and finish are likely to be the same.

One way to get around this problem is to repeat the calculation many times between the two calls to the clock function. For example, if you want to determine how long it takes to sort 10 numbers, you can perform the sort-10-numbers experiment 1000 times in a row and then divide the total elapsed time by 1000. This strategy gives you a timing measurement that is much more accurate. Part of your experimentation is figuring out how many times you need to perform the sort operation for different-sized inputs in order to get reasonably accurate results.

Tasks:

    You are to identify the time efficiency of each algorithm by doing the exercise specified above and plotting an excel plot similar to below, each curse corresponding to an algorithm. Make sure to explain which symbol represents which algorithm. Please also mark the horizontal axis as the size, vertical axis as the time (specify the unit of your time: ms, or s). 

WHAT TO SUBMIT TO YOUR INSTRUCTOR: